<?php

namespace app\api\model;

use think\Model;
use think\model\concern\SoftDelete;

class Article extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;

    public function category()
    {
        return $this->hasOne(Category::class, 'id', 'cate_id');
    }
    public function comments()
    {
        return $this->hasMany(Comment::class, 'art_id', 'id');
    }
    public function userInfo()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
    public function getImagesAttr($arr)
    {
        $arr = explode(',', $arr);
        foreach ($arr as $k => $v) {
            $path = app()->getRootPath() . 'public' . $v;
            if ($path != app()->getRootPath() . 'public') {
                if (empty($v) || !file_exists($path)) {
                    $arr[$k] = addHost('/static/images/noimg.png');
                }else{
                    $arr[$k] = addHost($v);
                }
            }else{
                unset($arr[$k]);
            }
        }
        return $arr;
    }
    // 判断是否付费
    public static function checkHasPay($art_id)
    {
        $res = self::where(['user_id' => getUid(), 'id' => $art_id])->find();
        return $res;
    }
}
